package com.birdboot.core;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket=socket;
    }
    public void run(){
        //1解析请求
        try {
            InputStream in = socket.getInputStream();
            //V1内容:读取请求行
            StringBuilder builder = new StringBuilder();
            char cur='a',pre='a';
            int d;
            while((d=in.read())!=-1){//单字节的读取
                cur = (char)d;
                if(pre==13&cur==10){//10表示回车二进制,13表示换行
                    break;
                }
                builder.append(cur);
                pre=cur;
            }
            String line = builder.toString().trim();
            System.out.println("请求行:"+line);
            //分解请求行
            String method;//请求方法
            String uri;//请求路径
            String protocol;//协议版本
            String[] str = line.split("\\s");
            method = str[0];
            uri = str[1];
            protocol = str[2];
            System.out.println("请求方法:"+method);
            System.out.println("请求路径:"+uri);
            System.out.println("协议版本:"+protocol);

        } catch (IOException e) {
            e.printStackTrace();
        }

        //2.处理请求
        //3.发送响应

    }
}
